package com.cskaoyan.service;

import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.MarketBrandExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.cskaoyan.service.AdminBrandService;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

public class AdminBrandServiceImpl implements AdminBrandService {

    @Override
    public CommonData query(int page, int limit, MarketBrand marketBrand, String sort, String order) {
        TransactionUtil.startTransaction();
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketBrandMapper mapper = sqlSession.getMapper(MarketBrandMapper.class);

        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();

        Integer id = marketBrand.getId();
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (marketBrand.getName() != null && marketBrand.getName() != ""){
            criteria.andNameEqualTo(marketBrand.getName());
        }
        marketBrandExample.setOrderByClause(sort+" "+order);
        PageHelper.startPage(page,limit);

        List<MarketBrand> marketBrands = mapper.selectByExample(marketBrandExample);

        PageInfo<MarketBrand> marketBrandPageInfo = new PageInfo<>(marketBrands);

        CommonData data = CommonData.data(marketBrandPageInfo);

        sqlSession.close();

        return data;
    }

    @Override
    public MarketBrand create(MarketBrand marketBrand) {
        TransactionUtil.startTransaction();
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketBrandMapper mapper = sqlSession.getMapper(MarketBrandMapper.class);
        marketBrand.setAddTime(new Date());
        marketBrand.setUpdateTime(new Date());
        marketBrand.setDeleted(false);
        mapper.insert(marketBrand);

        marketBrand.setId(this.getId());
        sqlSession.commit();
        return marketBrand;
    }

    @Override
    public MarketBrand update(MarketBrand marketBrand) {
        TransactionUtil.startTransaction();
        MarketBrandMapper marketBrandMapper = TransactionUtil.getMapper(MarketBrandMapper.class);
        marketBrand.setUpdateTime(new Date());
        marketBrandMapper.updateByPrimaryKey(marketBrand);
        TransactionUtil.commit();
        return marketBrand;
    }

    @Override
    public void delete(Integer id) {
        TransactionUtil.startTransaction();
        MarketBrandMapper mapper = TransactionUtil.getMapper(MarketBrandMapper.class);
        mapper.deleteByPrimaryKey(id);
        TransactionUtil.commit();
    }

    private Integer getId() {
        TransactionUtil.startTransaction();
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketBrandMapper mapper = sqlSession.getMapper(MarketBrandMapper.class);
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.createCriteria();
        List<MarketBrand> marketBrands = mapper.selectByExample(marketBrandExample);
        Integer maxId = 0;
        for (MarketBrand marketBrand : marketBrands) {
            if((int)maxId<(int)marketBrand.getId()){
                maxId = marketBrand.getId();
            }
        }

        TransactionUtil.commit();
        return maxId;
    }
}
